#! /bin/sh
#
# susan-ctl-dir This shell script takes care of starting and stopping
#		 the susan Director daemon
#
# description: Backup Archiving REcovery Open Sourced.
#
export PSCMD="@PSCMD@"
export PS="ps"
export AWK="@AWK@"
export PIDOF="@PIDOF@"
export PGREP="@PGREP@"

PIDDIR=${PIDDIR:-@piddir@}
SUSAN_DIRECTOR_BINARY="${SUSAN_DIRECTOR_BINARY:-@sbindir@/susan-dir}"
export SUSAN_CONFIG_DIR=${SUSAN_CONFIG_DIR:-@confdir@}
SUSAN_DIRECTOR_PORT=${SUSAN_DIRECTOR_PORT:-@dir_port@}
SUSAN_DIRECTOR_USER=${SUSAN_DIRECTOR_USER:-@dir_user@}
SUSAN_DIRECTOR_GROUP=${SUSAN_DIRECTOR_GROUP:-@dir_group@}
SUSAN_SCRIPT_DIR=${SUSAN_SCRIPT_DIR:-@scriptdir@}
Susan="@SUSAN@"

#
# Source the generic functions.
#
. "${SUSAN_SCRIPT_DIR}/susan-ctl-funcs"


case "$1" in
    start)
        if [ -x "${SUSAN_DIRECTOR_BINARY}" ]; then
            echo "Starting the $Susan Director daemon"
            OPTIONS=''
            if [ "${SUSAN_DIRECTOR_USER}" != '' ]; then
                OPTIONS="${OPTIONS} -u ${SUSAN_DIRECTOR_USER}"
            fi

            if [ "${SUSAN_DIRECTOR_GROUP}" != '' ]; then
                OPTIONS="${OPTIONS} -g ${SUSAN_DIRECTOR_GROUP}"
            fi

            if [ "${SUSAN_CONFIG_DIR}" != '' ]; then
                OPTIONS="${OPTIONS} -c ${SUSAN_CONFIG_DIR}"
            fi

            checkcfg
            if [ $? != 0 ]; then
                exit 1
            fi

            "${SUSAN_DIRECTOR_BINARY}" -v $2 $3 ${OPTIONS}
        fi
        ;;

    stop)
        if [ -x "${SUSAN_DIRECTOR_BINARY}" ]; then
            echo "Stopping the $Susan Director daemon"
            killproc "${SUSAN_DIRECTOR_BINARY}" "${SUSAN_DIRECTOR_PORT}"
        fi
        ;;

    restart)
        $0 stop
        sleep 5
        $0 start
        ;;

    status)
        [ -x "${SUSAN_DIRECTOR_BINARY}" ] && status "${SUSAN_DIRECTOR_BINARY}" "${SUSAN_DIRECTOR_PORT}"
        exit $?
        ;;

    *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
        ;;
esac

exit 0

